package com.birdboot.core;

import com.birdboot.http.HttpServletRequest;

import java.io.*;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 该线程任务负责与指定客户端进行一次HTTP交互
 *
 * 1:解析请求
 * 2:处理请求
 * 3:回复响应
 *
 */
public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }
    public void run() {
        try {
            //1解析请求
            HttpServletRequest request = new HttpServletRequest(socket);

            //2处理请求
            String path = request.getUri();
            System.out.println("抽象路径:"+path);

            File baseDir = new File(
               ClientHandler.class.getClassLoader().getResource(".").toURI()
            );
            File staticDir = new File(baseDir,"static");
            File file = new File(staticDir,path);

            //添加分支,根据file表示的文件是否实际存在决定实际发送还是发送404
            int statusCode;//状态代码
            String statusReason;//状态描述
            if(file.isFile()){//file表示的是一个实际存在的文件
                statusCode = 200;
                statusReason = "OK";
            }else{
                statusCode = 404;
                statusReason = "NotFound";
                file = new File(staticDir,"404.html");
            }

            //3发送响应
            //3.1发送状态行
            println("HTTP/1.1" + " " + statusCode + " " + statusReason);
            //3.2发送响应头
            println("Content-Type: text/html");
            println("Content-Length: "+file.length());
            //单独发送回车+换行表示响应头部分发送完毕
            println("");
            //3.3发送正文
            OutputStream out = socket.getOutputStream();
            FileInputStream fis = new FileInputStream(file);
            int len = 1;//记录每次实际读取的数据量
            byte[] buf = new byte[1024 * 10];//10kb
            while((len = fis.read(buf))!=-1){
                out.write(buf,0,len);
            }

        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        } finally {
            try {
                //HTTP协议要求,一次交互后要断开链接
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
        out.write(data);
        out.write(13);//发送回车符
        out.write(10);//发送换行符
    }


    public static void main(String[] args) {
        try {
            File baseDir = new File(
                ClientHandler.class.getClassLoader().getResource(".").toURI()
            );
            //定位类加载路径下的static目录
            File staticDir = new File(baseDir,"static");
            //定位static下的index.html
            File file = new File(staticDir,"index.html");

            System.out.println("是否存在:"+file.exists());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

}

